﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>新增编辑</title>
    <script src="/Apm/js/CMSEdit.js" type="text/javascript"></script>
    <style>
        
    </style>
</head>
<body>
    <div class="page">
        <div id="title" class="title">
            添加产品
        </div>
        <div class="layui-form layui-form-pane edit-form">
            <div class="layui-form-item">
                <label class="layui-form-label">产品标题</label>
                <div class="layui-input-block">
                    <input id="Title" type="text" name="Title" required lay-verify="required" placeholder="请输入产品标题" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">产品介绍</label>
                <div class="layui-input-block">
                    <textarea id="Introduce" name="Introduce" placeholder="请输入介绍" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item" pane>
                <label class="layui-form-label">显示</label>
                <div class="layui-input-block">
                    <input id="Isdisplay" type="checkbox" name="Isdisplay" lay-text="是|否" value="1" lay-skin="switch">
                </div>
            </div>
            <div class="layui-form-item" pane>
                <label class="layui-form-label">微信</label>
                <div class="layui-input-block">
                    <input id="Isweixin" type="checkbox" name="Isweixin" lay-text="是|否" value="1" lay-skin="switch">
                </div>
            </div>
            <button id="submit" lay-submit lay-filter="submit" style="display:none;"></button>
        </div>
        <!--图片上传-->
        <div class="edit-picture">
            <div class="upload-img">
                <div class="upload-img-button">
                    <span>产品图片</span>
                </div>
                <input type="file" name="file" style="display: none;" id="imgAnnex" />
                <div class="upload-imglist">
                    <!--<div class="imgrow">
                        <img src="/image/login.jpg" />
                        <div class="delete-img">
                            ×</div>
                    </div>
                    <div class="imgrow">
                        <img src="/image/login.jpg" />
                        <div class="delete-img">
                            ×
                        </div>
                    </div>-->
                    <div id="uploadimg" class="imgrow">
                        <img onclick="uploadimgadd()" src="/image/upload.png" />
                    </div>
                </div>
            </div>
            
        </div>

        <div class="edit-submit">
            <button id="truebutton" class="layui-btn layui-btn-lg">提交</button>
        </div>
    </div>
</body>
</html>
<script>
    var guid = MyPublic.getUrlParam('guid');
    
    layui.use(['layer', 'form'], function () {
        var layer = layui.layer,
            form = layui.form;

        
        var type = MyPublic.getUrlParam('type');
        switch (type) {
            case 'add':
                $("#title").html('添加产品');
                break;
            case 'edit':
                $("#title").html('编辑产品');
                GetBindData(guid);
                GetBindImage(guid, 'Product');
                break;
        }
        $("#truebutton").click(function () {
            $("#submit").click();
        })
        //产品添加或修改
        form.on('submit(submit)', function (data) {
            //console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}

            var json = data.field;
            json.Type = type;
            json.Guid = guid;
            json.CompanyCode = MyPublic.getCompanyCode();
            json.PersonalAdd = MyPublic.getUserCode();
            
            $.ajax({
                url: ApiURL + 'api/Method/Product?token=' + MyPublic.getToken(),
                type: 'post',
                contentType: 'application/json',
                data: JSON.stringify(json),
                success: function (data) {
                    data = JSON.parse(data);
                    if (data.code == "10020") {
                        
                        parent.AddTab('MenuProduct', '/Public/Product/ProductList.html', '<img src="/icon/computer_go.png" />', '产品管理');
                        parent.DeleteTab('PublicProductAdd');
                    }
                    else if (data.code == "10021") {
                        
                        parent.AddTab('MenuProduct', '/Public/Product/ProductList.html', '<img src="/icon/computer_go.png" />', '产品管理');
                        parent.DeleteTab('PublicProductEdit');
                    }
                    else {
                        layer.msg(data.msg);
                    }
                    
                },
                error: function () {
                    location.href = "/error.html";
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


    });
    function GetBindData(id) {
        layui.use(['layer', 'form'], function () {
            var layer = layui.layer,
                form = layui.form;

            $.ajax({
                url: ApiURL + 'api/Method/GetProductModel?token=' + MyPublic.getToken(),
                type: 'get',
                contentType: 'application/json',
                data: { guid: id },
                success: function (data) {
                    console.log(data);
                    $("#Title").val(data.Title);
                    $("#Introduce").val(data.Introduce);
                    if (data.Isdisplay == 1) {
                        $("#Isdisplay").attr('checked', true);
                    }
                    if (data.Isweixin == 1) {
                        $("#Isweixin").attr('checked', true);
                    }
                    form.render('checkbox');
                },
                error: function () {
                    location.href = "/error.html";

                }
            })
        })
        
    }

    /**
     * 上传图片
     */
    function uploadimgadd() {
        $("#imgAnnex").show();
        $("#imgAnnex").click();
        $("#imgAnnex").hide();
    }
    layui.use(['upload','layer'], function () {
        var layer = layui.layer,
            upload = layui.upload;

        var imagedata = new Object();
        imagedata.CompanyCode = MyPublic.getCompanyCode();
        imagedata.Master = guid;
        imagedata.Module = 'Product';
        //上传图片
        upload.render({
            elem: '#imgAnnex',
            url: ApiURL + 'api/Upload/Image',
            data: imagedata,
            accept: 'images',
            done: function (res) {
                if (res.code === "0") {
                    
                    $("#uploadimg").before('<div id="' + res.data.AnnexCode + '"  class="imgrow"><img id="view' + res.data.AnnexCode + '" data-url="' + res.data.AnnexUrl + '"  data-thumb="' + res.data.thumbnail + '"  data-name="' + res.data.AnnexOldName + '" onclick="ViewImage(this.id)"  src="' + ApiURL + res.data.thumbnail + '"><div onclick="deleteupload(\'' + res.data.AnnexCode + '\')" class="delete-img">×</div></div>');
                }
                else {
                    layer.msg(res.msg);
                }
                
            },
            error: function () {
                location.href = "/error.html";
            }
        })
        
    });
    
</script>